package chen.web.test;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import junit.framework.TestCase;

import chen.web.forum.*;
import chen.web.forum.SubjectCondition.SubjectType;
import chen.web.user.InternalAccountProvider;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/test.xml"})
public class TopicManagerTesting extends TestCase{
	@Autowired
	private TopicManager tm ;
	@Autowired
	private CategoryManager cm ;

	@Before 
	public void setUp() throws Exception {
		// TODO Auto-generated method stub
		SetCurrentAccount.set(InternalAccountProvider.getPublicGuestAccount());
	}

	@Test
	public void testPublishSubject() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, InstantiationException, NotLowestException{
		Category c = cm.get(5);
		Subject sub = new Subject(c);
/*		Account account = new Account();
		account.setId(7);
		account.setNickname("chenzhao");
		sub.setAuthor(account);*/
		sub.setContent("寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd");
		sub.setTitle("你寂寞吗ddddd要");
		sub = tm.post(sub);	
		Assert.isTrue(sub.getId() > 0);
	}
	
	@Test
	public void testList(){
		Category c = cm.get(2);
		SubjectCondition con = new SubjectCondition();
		con.setCategory(c);
		List<TopicInfo> list = tm.getTopicInfoList(con, 0, 10);	
		Assert.notEmpty(list);
		con.setType(SubjectType.good);
		list = tm.getTopicInfoList(con, 0, 10);	
		Assert.notEmpty(list);
		con.setType(SubjectType.top);
		list = tm.getTopicInfoList(con, 0, 10);	
		Assert.notEmpty(list);
	}
	
	@Test
	public void testAmount(){
		Category c = cm.get(2);
		SubjectCondition con = new SubjectCondition();
		con.setCategory(c);
		int n = tm.totalSubject(con);
		Assert.isTrue(n>0);
		ReplyCondition condition = new ReplyCondition().setCategory(c).setContainsChildren(true);
		n = tm.totalReply(condition);
		Assert.isTrue(n>0);
	}
	
	@Test
	public void testGetSubject(){
		Category c = cm.get(2);
		SubjectCondition con = new SubjectCondition();
		con.setCategory(c);
		List<TopicInfo> list = tm.getTopicInfoList(con, 0, 10);	
		Subject sub = tm.getSubject(list.get(0).getId());
		Assert.notNull(sub);
	}
	
	@Test
	public void testPublishReply(){
		Subject sub = tm.getSubject(460);
		Assert.notNull(sub);
		Reply reply = new Reply(sub);
		reply.setContent("寂寞哥在此ddddd寂寞哥在此ddddd寂寞哥在此ddddd寂");
		tm.post(reply);
	}

}
